package com.module.circle.ui.taskManage

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.common.app.base.AppActivity
import com.common.app.data.bean.BaseCityModel
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.KeyEvents
import com.common.app.data.bean.circle.CircleTaskModel
import com.common.app.utls.HeadTitleUtils
import com.common.app.utls.TimeUtils
import com.common.app.widget.CustomCommonRollerSelectDialog
import com.common.base.app.extras.getShowMsg
import com.common.base.app.extras.no
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes
import com.common.base.utils.StatusBarUtils
import com.common.base.utils.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.module.circle.R
import com.module.circle.databinding.CircleActivityTaskManageBinding

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/08/04
 * <p>
 * 描述：创建圈子任务页面
 * <p>
 * 修订历史：
 */
class TaskManageActivity : AppActivity<CircleActivityTaskManageBinding, TaskManageViewModel>(), View.OnClickListener{

    override fun getViewModel() = TaskManageViewModel::class.java
    override fun getViewContentBinding() = CircleActivityTaskManageBinding.inflate(layoutInflater)
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    @Autowired(name = KeyBundle.BUNDLE_KEY)
    @JvmField
    var circleTaskModel: CircleTaskModel?= null

    @Autowired(name = KeyBundle.EXPAND_DATA)
    @JvmField
    var circleId: Int= 0

    private val timeRollerSelectDialog  by lazy{ CustomCommonRollerSelectDialog(this) }
    private val motionCycleRollerSelectDialog  by lazy{ CustomCommonRollerSelectDialog(this) }
    private val daysExerciseRollerSelectDialog  by lazy{ CustomCommonRollerSelectDialog(this) }
    private val singleExerciseRollerSelectDialog  by lazy{ CustomCommonRollerSelectDialog(this) }

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)
        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(resources.getString(R.string.circle_ac_task_manage_title))
        headTitleUtils.setLineStyle(10)

    }

    override fun initEvents() {
        super.initEvents()

        mViewContentBinding.kvStartTime.setOnClickListener(onClick = this)
        mViewContentBinding.kvMotionCycle.setOnClickListener(onClick = this)
        mViewContentBinding.kvDaysExercise.setOnClickListener(onClick = this)
        mViewContentBinding.kvSingleExercise.setOnClickListener(onClick = this)
        mViewContentBinding.tvSaveBtn.setOnClickListener(onClick = this)

        timeRollerSelectDialog.setListener(object: CustomCommonRollerSelectDialog.OnSelectListener{
            override fun onSelectClick(mBean: BaseCityModel) {
                mViewContentBinding.kvStartTime.setValue(mBean.name)
                mViewContentBinding.kvStartTime.tag = mBean.code
            }
        })
        motionCycleRollerSelectDialog.setListener(object: CustomCommonRollerSelectDialog.OnSelectListener{
            override fun onSelectClick(mBean: BaseCityModel) {
                mViewContentBinding.kvMotionCycle.setValue(mBean.name)
                mViewContentBinding.kvMotionCycle.tag = mBean.code


                val daysExerciseItems = mutableListOf<BaseCityModel>()
                (2..mViewContentBinding.kvMotionCycle.tag.toString().toInt()).toList().forEach {
                    daysExerciseItems.add(BaseCityModel(name = "$it${getString(R.string.common_day_unit2)}" ,province = null,pinyin = null,pCode = it.toString(),code = it.toString()))
                }
                daysExerciseRollerSelectDialog.setData(daysExerciseItems)
                daysExerciseRollerSelectDialog.setTitle(getString(R.string.circle_days_exercise_hint))

                (mViewContentBinding.kvDaysExercise.tag != null).yes {
                    (mViewContentBinding.kvDaysExercise.tag.toString().toInt() < mBean.code.toInt()).yes {
                    }.otherwise {
                        mViewContentBinding.kvDaysExercise.setValue(mBean.name)
                        mViewContentBinding.kvDaysExercise.tag = mBean.code
                    }
                }
            }
        })

        daysExerciseRollerSelectDialog.setListener(object: CustomCommonRollerSelectDialog.OnSelectListener{
            override fun onSelectClick(mBean: BaseCityModel) {
                mViewContentBinding.kvDaysExercise.setValue(mBean.name)
                mViewContentBinding.kvDaysExercise.tag = mBean.code
            }
        })

        singleExerciseRollerSelectDialog.setListener(object: CustomCommonRollerSelectDialog.OnSelectListener{
            override fun onSelectClick(mBean: BaseCityModel) {
                mViewContentBinding.kvSingleExercise.setValue(mBean.name)
                mViewContentBinding.kvSingleExercise.tag = mBean.code
            }
        })
    }

    override fun lazyInitData() {
        super.lazyInitData()

        mViewContentBinding.kvTaskType.setValue("跑步")
        mViewContentBinding.kvTaskType.tag = "running"

        (circleTaskModel != null).yes {
            mViewContentBinding.tvCircleIntroductionHint.text = getString(R.string.circle_modify_task_hint)
            mViewContentBinding.tvSaveBtn.text = getString(R.string.common_confirm_modify_hint)
            mViewContentBinding.kvStartTime.setValue(TimeUtils.getYearMonthDay(circleTaskModel?.taskinfo?.start_date?:0L))
            mViewContentBinding.kvStartTime.tag = circleTaskModel?.taskinfo?.start_date

            mViewContentBinding.kvMotionCycle.setValue("${circleTaskModel?.taskinfo?.cycle}${getString(R.string.common_day_unit2)}")
            mViewContentBinding.kvMotionCycle.tag = circleTaskModel?.taskinfo?.cycle

            mViewContentBinding.kvDaysExercise.setValue("${circleTaskModel?.taskinfo?.days_req}${getString(R.string.common_day_unit2)}")
            mViewContentBinding.kvDaysExercise.tag = circleTaskModel?.taskinfo?.days_req

            mViewContentBinding.kvSingleExercise.setValue("${circleTaskModel?.taskinfo?.metric_value}${getString(R.string.common_kilometer_splice)}")
            mViewContentBinding.kvSingleExercise.tag = circleTaskModel?.taskinfo?.metric_value
        }.otherwise {

        }

        val timeItems = mutableListOf<BaseCityModel>()
        TimeUtils.getRemainingDaysOfMonth(System.currentTimeMillis(),30).forEach {
            timeItems.add(BaseCityModel(name = TimeUtils.getYearMonthDay(it) ,province = null,pinyin = null,pCode = it.toString(),code = it.toString()))
        }
        timeRollerSelectDialog.addData(timeItems)
        timeRollerSelectDialog.setTitle(getString(R.string.circle_start_time_hint))


        val motionCycleItems = mutableListOf<BaseCityModel>()
        (7..30).toList().forEach {
            motionCycleItems.add(BaseCityModel(name = "$it${getString(R.string.common_day_unit2)}" ,province = null,pinyin = null,pCode = it.toString(),code = it.toString()))
        }
        motionCycleRollerSelectDialog.addData(motionCycleItems)
        motionCycleRollerSelectDialog.setTitle(getString(R.string.circle_motion_cycle_hint))



        val daysExerciseItems = mutableListOf<BaseCityModel>()
        (2..30).toList().forEach {
            daysExerciseItems.add(BaseCityModel(name = "$it${getString(R.string.common_day_unit2)}" ,province = null,pinyin = null,pCode = it.toString(),code = it.toString()))
        }
        daysExerciseRollerSelectDialog.addData(daysExerciseItems)
        daysExerciseRollerSelectDialog.setTitle(getString(R.string.circle_days_exercise_hint))


        val singleExerciseItems = mutableListOf<BaseCityModel>()
        (1..15).toList().forEach {
            singleExerciseItems.add(BaseCityModel(name = "$it${getString(R.string.common_kilometer_splice)}" ,province = null,pinyin = null,pCode = it.toString(),code = it.toString()))
        }
        singleExerciseRollerSelectDialog.addData(singleExerciseItems)
        singleExerciseRollerSelectDialog.setTitle(getString(R.string.circle_single_exercise_mileage_hint))
    }

    override fun onClick(v: View?) {
        when (v) {
            mViewContentBinding.kvStartTime->{
                timeRollerSelectDialog.isShowing.no {
                    timeRollerSelectDialog.show()
                }
            }
            mViewContentBinding.kvMotionCycle->{
                motionCycleRollerSelectDialog.isShowing.no {
                    motionCycleRollerSelectDialog.show()
                }

            }
            mViewContentBinding.kvDaysExercise->{
                if ((mViewContentBinding.kvMotionCycle.tag == null)){
                    showToast(R.string.circle_motion_cycle_completed_hint)
                    return
                }
                daysExerciseRollerSelectDialog.isShowing.no {
                    daysExerciseRollerSelectDialog.show()
                }
            }
            mViewContentBinding.kvSingleExercise->{
                singleExerciseRollerSelectDialog.isShowing.no {
                    singleExerciseRollerSelectDialog.show()
                }
            }
            mViewContentBinding.tvSaveBtn->requestConfigCirclesTask()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }


    private fun requestConfigCirclesTask() {

        if (circleId <= 0) {
            showErrorMsg()
            return
        }

        if (mViewContentBinding.kvStartTime.tag == null){
            showToast(getString(R.string.circle_start_time_completed_hint))
            return
        }
        if (mViewContentBinding.kvMotionCycle.tag == null){
            showToast(getString(R.string.circle_motion_cycle_completed_hint))
            return
        }

        if (mViewContentBinding.kvDaysExercise.tag == null){
            showToast(getString(R.string.circle_days_exercise_completed_hint))
            return
        }
        if (mViewContentBinding.kvSingleExercise.tag == null){
            showToast(getString(R.string.circle_single_exercise_mileage_completed_hint))
            return
        }

        showDialogLoading()
        mViewModel.requestConfigCirclesTask(circleId,mViewContentBinding.kvMotionCycle.tag.toString().toInt(),mViewContentBinding.kvDaysExercise.tag.toString().toInt(),
            mViewContentBinding.kvSingleExercise.tag.toString().toDouble(),mViewContentBinding.kvStartTime.tag.toString().toLong(),"running",){
            showToast(it.getShowMsg())
        }.observe(this) {
            dismissLoadingDialog()
            it.isSuccess().yes {
                LiveEventBus.get(KeyEvents.KEY_REFRESH_CIRCLE_DETAILS_EVENT).post(this)
                showToast(getString(R.string.circle_configuration_task_completed_hint))
                finish()
            }.otherwise {
                showToast(it.Message)
            }
        }
    }
}